using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Systems.CMS;
using Deepcode.Flux.Core.Systems.CMS.TemplateGallery;
using Deepcode.Flux.Core.Systems.Workflow.Bases;

public partial class Private_CMS_main_actions_submit_chooseDef : Deepcode.Flux.Core.UI.CMS.CMSActionPane
{
	#region Page Load Event
	/// <summary>
	/// Page Load event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			Definition [] available = new Definitions().GetDefinitionsByHandlerID("ContentManagement");
			if (available.Length == 0)
			{
				// There are no workflows - publish it
				ContentManager cms = new ContentManager();
				ContentObject obj = cms.AdxGetByID( ContentID );
				if (obj != null)
				{
					cms.SubmitForApproval(ContentID, -1, CurrentUser.PK_ID);
					// Audit
					Deepcode.Flux.Core.Systems.Audit.Quick.QuickAudit.RaiseInformation(new Deepcode.Flux.Core.Systems.Audit.Quick.AuditUser(),
						"Flux",
						"Content Manager",
						"submitted for approval - {0}", obj.NodeTitle);
				}

				GoBack( true );
			}
			else if (available.Length == 1)
			{
				// There is only one - publish using that
				ContentManager cms = new ContentManager();
				ContentObject obj = cms.AdxGetByID(ContentID);
				if (obj != null)
				{
					cms.SubmitForApproval(ContentID, available[0].PK_ID, CurrentUser.PK_ID);
					// Audit
					Deepcode.Flux.Core.Systems.Audit.Quick.QuickAudit.RaiseInformation(new Deepcode.Flux.Core.Systems.Audit.Quick.AuditUser(),
						"Flux",
						"Content Manager",
						"submitted for approval - {0}", obj.NodeTitle);
				}
				GoBack(true);
			}
			else
			{
				fWorkflows.Items.Clear();
				foreach (Definition d in available)
					fWorkflows.Items.Add(new ListItem(d.DefName, d.PK_ID.ToString()));
			}
		}
	}
	#endregion
	#region Interactions
	/// <summary>
	/// Type selected, go forward to the actual editor form itself
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnNext_Click(object sender, EventArgs e)
	{
		if( ! Page.IsValid ) return;

		ContentManager cms = new ContentManager();
		ContentObject o = cms.AdxGetByID( this.ContentID );
		if( o == null ) GoBack();

		try
		{
			int selectedDef = Int32.Parse(fWorkflows.SelectedValue);
			cms.SubmitForApproval(o.PK_ID, selectedDef, CurrentUser.PK_ID);

			Deepcode.Flux.Core.Systems.Audit.Quick.QuickAudit.RaiseInformation(new Deepcode.Flux.Core.Systems.Audit.Quick.AuditUser(),
				"Flux",
				"Content Manager",
				"submitted for approval - {0}", o.NodeTitle);
		}
		catch { }

		// Return back to list
		GoBack( true );
	}
	/// <summary>
	/// Cancel clicked - go back to content view listing
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnCancel_Click(object sender, EventArgs e)
	{
		GoBack();
	}
	#endregion
}
